package com.wh.minio.container.filter;


import com.wh.minio.container.common.MinioClientUtils;
import com.wh.minio.container.common.Utils;
import io.minio.MinioClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * 配置[请求]过滤器
 * 主要功能：服务器访问资源，拦截非[桶]资源信息
 * @Package com.wh.minio.container.filter
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class FileResourceFilter implements Filter {


    private static final Log logger = LogFactory.getLog(FileResourceFilter.class);

    /**
     * 构造函数
     */
    public FileResourceFilter() {

    }

    /**
     * 销毁
     */
    public void destroy() {
        logger.info("destroy");
    }

    /**
     * 初始化
     * @param filterConfig
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.info("init");
    }

    /**
     * 执行拦截
     * @param servletRequest
     * @param servletResponse
     * @param chain
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain){
       try{
            logger.info("doFilter");
            //获取HttpServletRequest
            HttpServletRequest request = (HttpServletRequest) servletRequest;
            //获取HttpServletResponse
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            //获取MinioClient对象
            MinioClient minioClient = MinioClientUtils.getInstance().getMinioClient();
            String requestURL  = request.getRequestURI().substring(1, request.getRequestURI().length());
            //先判断拦截请求, 根目录请求(不处理), 资源请求
           int count = Utils.countStr(requestURL, "/");
           if(count > 2){
               requestURL = requestURL.substring(requestURL.indexOf("/") + 1, requestURL.length());
               requestURL = requestURL.substring(requestURL.indexOf("/") + 1, requestURL.length());
               //获取桶的名称
               String bucket = requestURL.substring(0, requestURL.indexOf("/"));
               //获取通的资源路径
               String filePath = requestURL.substring(requestURL.indexOf("/") + 1, requestURL.length());
               //判断桶是否存在
               if(MinioClientUtils.getInstance().isExistbucket(bucket)){
                   logger.info(bucket + " 桶存在!");
                   //判断桶对应资源是否存在
                   if(MinioClientUtils.getInstance().filePathExist(bucket, filePath)){
                       String url = minioClient.presignedGetObject(bucket, filePath, 60 * 60 * 24);
                       logger.info(bucket + " 桶存在! 文件信息： " + filePath + ", 重定向URL： " + url);
                       response.sendRedirect(url);
                   }
               }else{
                   logger.warn(bucket + " 桶不存在! 无法获取资源!!");
               }
           }
        }catch (IOException e){
            e.printStackTrace();
            logger.error("doFilter-->>IOException" + e.getMessage());
        }catch (ServletException e){
            e.printStackTrace();
            logger.error("doFilter-->>ServletException" + e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("doFilter-->>Exception" + e.getMessage());
       }
    }

}
